package org.loofer.uikit;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import org.loofer.uikit.text.LengthConverter;
import org.loofer.uikit.text.LengthFilter;

public class ExtendEditText extends EditText {
    private boolean mClearFocusOnBack = false;
    private LengthConverter mLengthConverter = null;
    private LimitListener mLimitListener;

    final class InnerLengthFilter extends LengthFilter {
        public InnerLengthFilter(int maxLength) {
            super(maxLength, ExtendEditText$InnerLengthFilter$$Lambda$1.lambdaFactory$(ExtendEditText.this));
        }

        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            setLengthConverter(ExtendEditText.this.mLengthConverter);
            return super.filter(source, start, end, dest, dstart, dend);
        }
    }

    public interface LimitListener {
        void onMaxLengthReached(int i);
    }

    public ExtendEditText(Context context) {
        super(context);
        init(context, null);
    }

    public ExtendEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ExtendEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (context != null && attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExtendEditText);
            setClearFocusOnBack(a.getBoolean(R.styleable.ExtendEditText_clearFocusOnBack, false));
            setMaxLength(a.getInteger(R.styleable.ExtendEditText_android_maxLength, -1));
            a.recycle();
        }
    }

    public void setClearFocusOnBack(boolean clear) {
        this.mClearFocusOnBack = clear;
    }

    public void setMaxLength(int maxLength) {
        if (maxLength >= 0) {
            setFilters(new InputFilter[]{new InnerLengthFilter(maxLength)});
        }
    }

    public void setLengthConverter(LengthConverter converter) {
        this.mLengthConverter = converter;
    }

    public void setLimitListener(LimitListener listener) {
        this.mLimitListener = listener;
    }

    public int getConvertedLength() {
        CharSequence text = getText();
        if (TextUtils.isEmpty(text)) {
            return 0;
        }
        LengthConverter converter = this.mLengthConverter;
        if (converter == null) {
            return text.length();
        }
        return converter.convert(text, 0, text.length());
    }

    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (!this.mClearFocusOnBack || keyCode != 4 || event.getAction() != 1) {
            return super.onKeyPreIme(keyCode, event);
        }
        hideIme();
        clearFocus();
        return true;
    }

    private void hideIme() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService("input_method");
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindowToken(), 0);
        }
    }

    private void notifyMaxLengthReached(int maxLength) {
        LimitListener limitListener = this.mLimitListener;
        if (limitListener != null) {
            limitListener.onMaxLengthReached(maxLength);
        }
    }
}
